package com.lxn.course.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lxn.course.domain.Course;
import com.lxn.course.domain.CourseDetail;
import com.lxn.course.domain.CourseMarket;
import com.lxn.course.dto.CourseSaveDTO;
import com.lxn.course.mapper.CourseDetailMapper;
import com.lxn.course.mapper.CourseMapper;
import com.lxn.course.mapper.CourseMarketMapper;
import com.lxn.course.query.CourseQuery;
import com.lxn.course.service.ICourseService;
import com.lxn.doc.CourseDoc;
import com.lxn.hrm.feignclients.ESFeignClient;
import com.lxn.hrm.util.AjaxResult;
import com.lxn.hrm.util.LoginInfo;
import com.lxn.hrm.util.PageList;
import com.lxn.hrm.util.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author hwerth
 * @since 2020-02-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseDetailMapper courseDetailMapper ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseSaveDTO courseSaveDTO) {
        //保存
        Course course = courseSaveDTO.getCourse();

        //状态
        course.setStatus(Course.STATUS_OFFLINE);
        //开课时间
        Date date = new Date();
        course.setStartTime(date);
        //结课时间
        course.setEndTime(date);

        LoginInfo loginInfo = UserContext.getLoginInfo();
        course.setTenantId(loginInfo.getTenantId());
        //机构名称
        course.setTenantName(loginInfo.getTenantName());

        course.setUserId(loginInfo.getUserId());

        course.setUserName(loginInfo.getUserName());

        baseMapper.insert(course);

        //课程详情
        CourseDetail courseDetail = courseSaveDTO.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);

        //课程营销
        CourseMarket courseMarket = courseSaveDTO.getCourseMarket();

        courseMarket.setId(course.getId());
        courseMarket.setPriceOld(courseMarket.getPrice());

        //截止时间
        courseMarket.setExpires(date);

        courseMarketMapper.insert(courseMarket);

    }


    @Override
    public void onLineCourse(Long id) {
        //根据id查询课程
        Course course = baseMapper.selectById(id);

        //判断课程状态
        if(course == null || course.getStatus() != Course.STATUS_OFFLINE){
            throw new RuntimeException("课程未上线!");
        }

        //课程营销
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        CourseDetail courseDetail = courseDetailMapper.selectById(id);

        CourseDoc courseDoc = new CourseDoc();
        //课程属性
        BeanUtils.copyProperties(course , courseDoc);
        //营销属性
        BeanUtils.copyProperties(courseMarket , courseDoc);
        //详情属性
        BeanUtils.copyProperties(courseDetail , courseDoc);

        courseDoc.setChargeName(courseMarket.getCharge()==0?"免费":"收费");
        //预览数量
        courseDoc.setViewCount(100);
        //购买数量
        courseDoc.setBuyCount(200);

        AjaxResult ajaxResult = esFeignClient.save(courseDoc);
        if(ajaxResult.isSuccess()){
            //修改课程状态为上线
            course.setStatus(Course.STATUS_ONLINE);
            baseMapper.updateById(course);
        }else{
            throw new RuntimeException("课程上线失败");
        }
    }

    @Override
    public void offLineCourse(Long id) {
        //1.根据id查询课程
        Course course = baseMapper.selectById(id);

        //判断课程为上线
        if(course == null || course.getStatus() != Course.STATUS_ONLINE){
            throw new RuntimeException("课程未上线!");
        }

        //3.删除ES中的课程
        AjaxResult ajaxResult = esFeignClient.deleteById(id);

        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("课程下线失败");
        }

        //4.修改mysql中的课程状态为：下线
        course.setStatus(Course.STATUS_OFFLINE);
        baseMapper.updateById(course);
    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
